package com.szms.mobile.me;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.szms.mobile.BaseFragment;
import com.szms.mobile.MainActivity;
import com.szms.mobile.R;
import com.szms.mobile.enums.SystemConstants;
import com.szms.mobile.repository.SysCfgRepository;
import com.szms.mobile.repository.model.SysCfgModel;

import java.time.LocalDateTime;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * create an instance of this fragment.
 */
public class SystemFragment extends BaseFragment {

    EditText txtRemoteAddr;
    EditText txtRemotePort;

    public SystemFragment(MainActivity _activity) {
        super(_activity, false);
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_system, container, false);
    }

    @Override
    public void onResume() {
        super.onResume();
        txtRemoteAddr = activity.findViewById(R.id.txtRemoteAddr);
        txtRemotePort = activity.findViewById(R.id.txtRemotePort);

        TextView btnSaveSystem = activity.findViewById(R.id.btnSaveSystem);
        //添加保存按钮事件
        btnSaveSystem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnSaveSystem.setEnabled(false);
                saveParameters();
                btnSaveSystem.setEnabled(true);
            }
        });

        this.readParameters();
    }

    private  void readParameters(){
        SysCfgRepository repository = new SysCfgRepository(activity);
        List<SysCfgModel> host = repository.query("cfg_key=?", new String[]{SystemConstants.RemoteHost.getCode()});
        if(host.size()>0){
            SysCfgModel item = host.get(0);
            txtRemoteAddr.setText(item.getConfigValue());
        }
        List<SysCfgModel> port = repository.query("cfg_key=?", new String[]{SystemConstants.RemotePort.getCode()});
        if (port.size()>0){
            SysCfgModel item = port.get(0);
            txtRemotePort.setText(item.getConfigValue());
        }

    }

    private void saveParameters() {
        SysCfgRepository repository = new SysCfgRepository(activity);

        setParameter(repository,txtRemoteAddr,SystemConstants.RemoteHost);

        setParameter(repository, txtRemotePort, SystemConstants.RemotePort);
    }

    /**
     * 设置值
     * @param repository
     * @param editText
     * @param constant
     */
    private void setParameter(SysCfgRepository repository,  EditText editText,SystemConstants constant) {
        String text = editText.getText().toString();
        if(text==null || text.equals(" ")){
            return;
        }
        List<SysCfgModel> models = repository.query("cfg_key=?", new String[]{constant.getCode()});
        if (models.size()>0){
            SysCfgModel item = models.get(0);
            item.setConfigValue(text);
            repository.update(item);
        }else{
            SysCfgModel item = new SysCfgModel();
            item.setConfigKey(constant.getCode());
            item.setConfigValue(text);
            item.setCreatedDate(LocalDateTime.now());
            item.setModifiedDate(LocalDateTime.now());
            repository.add(item);
        }
        Toast.makeText(activity.getBaseContext(), "save success",Toast.LENGTH_SHORT).show();
    }
}